package com.lm.boot;

import com.lm.boot.dao.GirlMapper;
import com.lm.boot.entity.Girl;
import com.lm.boot.entity.GirlProperties;
import com.lm.boot.error.UnifyException;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@EnableAutoConfiguration
@ComponentScan("com.lm.boot")
@RestController
public class HelloController {

    @Autowired
    private GirlProperties girlProperties;

    @Autowired
    private GirlMapper girlMapper;

    @Value("${user.name}")
    private String s;

    @Value("${user.name2}")
    private String ss;

    @ApiOperation(value = "演示第一个请求",notes = "")
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String say(@RequestParam Integer id) {
        return girlProperties.toString()+id;
    }

    @ApiOperation(value = "演示自动配置之自定义属性",notes = "根据注解@ConfigurationPeoperties对应自定义属性的前缀")
    @ApiImplicitParam(name = "girl", value = "女生数据实体girl", required = true, dataType = "Girl")
    @GetMapping(value = "/girl")
    public List<Girl> getGirl(){
        return girlMapper.findAll();
    }

    @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
    })
    @PostMapping(value = "/add-girl")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize, @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setAge(age);
        girl.setCupSize(cupSize);

        return  girlMapper.save(girl);
    }

    @GetMapping(value = "/girl/{id}")
    public Girl findGirl(@PathVariable("id") Integer id){
        return girlMapper.findOne(id);
    }

    @RequestMapping(value = "/user")
    public Map<String,String> findUser(){
        Map<String,String> map = new HashMap<>();
        map.put("name1:",s);
        map.put("name2:",ss);
        return map;
    }

    @RequestMapping(value = "/testerror")
    public String simuliteError() throws UnifyException {
        System.out.println(s);
        throw new UnifyException("系统错误，请联系管理人员");
//        int i = 10;
//        int r = i/0;
//        return String.valueOf(r);
    }

    public static void main(String[] args) {
        SpringApplication.run(HelloController.class,args);
    }
}
